/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.policy;


import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.uml2.uml.StateInvariant;

import com.cea.papyrus.diagram.sequence.sedi.model.command.SetStateInvariantCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Edit policy to handle direct edit of the name of StateInvariants. StateInvariants are
 * different from normal NamedElements because the edit text is the invariants Expression,
 * not the NamedElement Name
 * <p>
 * The policy requires the figure to implement ILabeledFigure.
 * </p>
 * 
 * @author frankd@ifi.uio.no
 */
public class StateInvariantDirectEditPolicy extends LabelDirectEditPolicy {

    @SuppressWarnings("unchecked")
    @Override
    protected Command getDirectEditCommand(DirectEditRequest request) {
        final GraphElement siView = (GraphElement) getHost().getModel();

        final SetStateInvariantCommand cmd = new SetStateInvariantCommand();
        cmd.setLabel("Edit StateInvariant Constraint");
        cmd.setStateInvariant(ModelUtil.<StateInvariant> getSemantic(siView));
        cmd.setExpressionString((String) request.getCellEditor().getValue());
        return cmd;
    }
}
